﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Competition : System.Web.UI.Page
{
    PagedDataSource objPds = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        MasterPage master = this.Master as MasterPage;
        master.BodyId = "competition";

        Session["page"] = "competition";


        //get upcoming competitions

        Load_Upcoming();
      
        //get ongoing competitions

      Load_Ongoing();
      
        //get previous competitions

      LoadPrevious();

      if (dtl_Ongoing.Items.Count == 0) lblOngoing_.Text = "Don't Have Any Ongoing Competitions !";
      if (dtl_Previous.Items.Count == 0)
      {
          lbl_Prev.Text = "Don't Have Any Completed Competitions !";
          
      }
      if (dtl_Upcoming.Items.Count == 0) lblUpcoming_.Text = "Don't Have Any Upcoming Competitions !";
    }

     
    void Load_Upcoming()
    {
        CompetitionDAO comDAO = new CompetitionDAO();
        AwardDAO awardDAO = new AwardDAO();
        dtl_Upcoming.DataSource = comDAO.SelectUpcoming();
        dtl_Upcoming.DataBind();
        if (dtl_Upcoming.Items.Count > 0)
        {
            foreach (DataListItem item in dtl_Upcoming.Items)
            {

                Label competitionID = item.FindControl("lblCompetitionID") as Label;
                int competID = Convert.ToInt32(competitionID.Text);


                DataList datalist = (DataList)item.FindControl("dtl_Awards_Upcoming");
                datalist.DataSource = awardDAO.AwardOfCompetition(competID);
                datalist.DataBind();
            }
        }

    }

    void Load_Ongoing()
    {
        CompetitionDAO comDAO = new CompetitionDAO();
        AwardDAO awardDAO = new AwardDAO();
        dtl_Ongoing.DataSource = comDAO.SelectOngoing();
        dtl_Ongoing.DataBind();
        if (dtl_Ongoing.Items.Count > 0)
        {
            foreach (DataListItem item in dtl_Ongoing.Items)
            {
                Label competitionID = item.FindControl("lblCompetitionID") as Label;
                int competID = Convert.ToInt32(competitionID.Text);

                DataList datalist = (DataList)item.FindControl("dtl_Awards_Ongoing");
                datalist.DataSource = awardDAO.AwardOfCompetition(competID);
                datalist.DataBind();
            }
        }
    }

    /*
    void Load_Previous()
    {
        CompetitionDAO comDAO = new CompetitionDAO();
        AwardDAO awardDAO = new AwardDAO();

        dtl_Previous.DataSource = comDAO.SelectPrevious();
        dtl_Previous.DataBind();
        if (dtl_Previous.Items.Count > 0)
        {
            foreach (DataListItem item in dtl_Previous.Items)
            {
                Label competitionID = item.FindControl("lblCompetitionID") as Label;
                int competID = Convert.ToInt32(competitionID.Text);

                DataList datalist = (DataList)item.FindControl("dtl_Awards_Previous");
                datalist.DataSource = awardDAO.AwardOfCompetition(competID);
                datalist.DataBind();
            }
        }
    }

    */

    private void LoadPrevious()
    {

        CompetitionDAO comDAO = new CompetitionDAO();
        AwardDAO awardDAO = new AwardDAO();

    

        objPds = new PagedDataSource();
        objPds.DataSource = comDAO.SelectPrevious();
        objPds.AllowPaging = true;
        objPds.PageSize = 3;

        try
        {
            objPds.CurrentPageIndex = Int32.Parse(Request["Page"].ToString());
        }
        catch (Exception ex)
        {
            objPds.CurrentPageIndex = 0;
        }

        lblCurrentPage.Text = "Page " + (objPds.CurrentPageIndex + 1).ToString() + " of "
            + objPds.PageCount.ToString();

        if (!objPds.IsFirstPage)
            cmdNext.Enabled = !objPds.IsLastPage;

        if (!objPds.IsLastPage)
            cmdPrev.Enabled = !objPds.IsFirstPage;


        dtl_Previous.DataSource = objPds;
        dtl_Previous.DataBind();

        if (dtl_Previous.Items.Count > 0)
        {
            foreach (DataListItem item in dtl_Previous.Items)
            {
                Label competitionID = item.FindControl("lblCompetitionID") as Label;
                int competID = Convert.ToInt32(competitionID.Text);

                DataList datalist = (DataList)item.FindControl("dtl_Awards_Previous");
                datalist.DataSource = awardDAO.AwardOfCompetition(competID);
                datalist.DataBind();
            }
        }

    }


    protected void btn_AllCompetitions_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = true;
        Panel3.Visible = true;
    }
    protected void btn_UpcomingCompetitions_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;

    }
    protected void btn_OngoingCompetitions_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
    }
    protected void btn_CompletedCompetitions_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
    }
    protected void cmdNext_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (objPds.CurrentPageIndex + 1));
      
    }
    protected void cmdPrev_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (objPds.CurrentPageIndex - 1));
    }
    protected void dtl_Previous_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnJoin_Click(object sender, EventArgs e)
    {
        if (Session["roleID"] != null)
        {
            if (Session["roleID"].ToString().Equals("4"))
            {
                Response.Redirect("~/Admin/UploadPosting.aspx");
            }
        }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Your must Login with Student Account!');", true);
            
    }
}